#ifndef __UDP_SERVER_HPP__
#define __UDP_SERVER_HPP__
#include <iostream>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "UdpServer.hpp"
#include "MyPthread/Log.hpp"
#include "MyPthread/ThreadPool.hpp"
#include "Common.hpp"
#include "InetAddr.hpp"

using namespace LogModule;
using namespace ThreadPoolMoudule;

const static int gsockfd = -1;
// const static std::string gdefaultip = "127.0.0.1"; // 表示本地主机
using func_t = std::function<std::string(const std::string &)>;
using add_user_t = std::function<void(const InetAddr &)>;
using task_t = std::function<void()>;
using route_t = std::function<void(int sockfd, const std::string &message)>;
using remove_t = std::function<void(const InetAddr &)>;

class nocopy
{
public:
    nocopy() {}
    nocopy(const nocopy &) = delete;
    const nocopy &operator=(const nocopy &) = delete;
    ~nocopy() {}
};
class UdpServer : public nocopy
{
private:
public:
    UdpServer(uint16_t port = server_port)
        : _sockfd(gsockfd), _server(port), _isrunning(false)
    {
    }
    ~UdpServer()
    {
    }
    // 1. 创建socket
    void InitServer()
    {
        // 1. 创建socket
        _sockfd = ::socket(AF_INET, SOCK_DGRAM, 0);

        // bind绑定
        int rn = ::bind(_sockfd, CONV(_server.NetAddr()), _server.NetAddrLen());
        if (rn < 0)
        {
            LOG(LogLevel::FATAL) << "bind failed";
            Die(1);
        }
        LOG(LogLevel::INFO) << "bind success";
    }

    void RegisterSerive(add_user_t adduser, route_t route, remove_t removeuser)
    {
        _add_user = adduser;
        _route = route;
        _removeuser = removeuser;
    }

    void Start()
    {
        _isrunning = false;
        // 2.2 对端地址，定义对端数据结构
        struct sockaddr_in peer;
        while (true)
        {
            socklen_t len = sizeof(peer);

            char buffer[1024];
            int n = ::recvfrom(_sockfd, buffer, sizeof(buffer) - 1, 0, CONV(&peer), &len);

            if (n > 0)
            {
                InetAddr client(peer);

                buffer[n] = 0;


                std::string message;

                if (strcmp(buffer, "QUIT") == 0)
                {
                    message = client.Addr() + " # " + "我走了哈，你们慢慢聊";
                    _removeuser(client);
                }
                else
                {
                    // 1.先添加用户，将用户置为在线
                    _add_user(client);
                    // LOG(LogLevel::INFO) << "add_user:" << client.Addr();
                    message = client.Addr() + " # " + buffer;
                }

                // 2.转发用户消息
                task_t task = std::bind(_route, _sockfd, message);

                ThreadPool<task_t>::getInstance()->Enqueue(task);

                // ::sendto(_sockfd, result.c_str(), result.size(), 0, CONV(client.NetAddr()), client.NetAddrLen());
            }
            // else
            // {
            //     LOG(LogLevel::INFO) << "recv nothing";
            // }
        }
        _isrunning = false;
    }

private:
    int _sockfd;
    InetAddr _server;
    // InetAddr _client;
    bool _isrunning; // 服务器运行状态

    // 业务

    // // 新增用户
    add_user_t _add_user;
    // // 移除用户
    remove_t _removeuser;
    // // 数据转发
    route_t _route;
};

#endif